package com.hy.mvc.biz.utils;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;

import java.text.MessageFormat;
import java.util.ArrayList;

/**
 * 格式化的Log工具，方便日志内容拼装过程
 *
 * @author
 */
public class LoggerUtils {

    public static final String LOG_SPACE = "ebk";

    public static final String LOG_TAGE_SETTLE = "settle";
    public static final String LOG_TAGE_FILTER = "filter";
    public static final String LOG_TAGE_ORDER = "order";
    public static final String LOG_TAGE_OFFLNE_EXPORT = "export";
    public static final String LOG_TAGE_LOGGER = "logger";
    public static final String LOG_TAGE_RATE = "rate";
    public static final String LOG_TAGE_RATE_LIMIT = "rateLimit";

    public static final String LOG_INVENTORY_LOGGER = "inventory";
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 输出info level的log信息.
     *
     * @param logger  日志记录器
     * @param message log信息,如:<code>xxx{0},xxx{1}...</code>
     * @param params  log格式化参数,数组length与message参数化个数相同, 如: <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     */
    public static void info(Logger logger, String message, Object... params) {
        if (logger.isInfoEnabled()) {
            logger.info(format(message, params));
        }
    }

    /**
     * 输出info level的log信息.
     *
     * @param throwable 异常对象
     * @param logger    日志记录器
     * @param message   log信息,如:<code>xxx{0},xxx{1}...</code>
     * @param params    log格式化参数,数组length与message参数化个数相同, 如: <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     */
    public static void info(Throwable throwable, Logger logger, String message, Object... params) {
        if (logger.isInfoEnabled()) {
            logger.info(format(message, params), throwable);
        }
    }

    /**
     * 输出warn level的log信息.
     *
     * @param logger  日志记录器
     * @param message log信息,如:<code>xxx{0},xxx{1}...</code>
     * @param params  log格式化参数,数组length与message参数化个数相同, 如: <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     */
    public static void warn(Logger logger, String message, Object... params) {
        if (logger.isWarnEnabled()) {
            logger.warn(format(message, params));
        }
    }

    /**
     * 输出warn level的log信息.
     *
     * @param throwable 异常对象
     * @param logger    日志记录器
     * @param message   log信息,如:<code>xxx{0},xxx{1}...</code>
     * @param params    log格式化参数,数组length与message参数化个数相同, 如: <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     */
    public static void warn(Throwable throwable, Logger logger, String message, Object... params) {
        if (logger.isWarnEnabled()) {
            logger.warn(format(message, params), throwable);
        }
    }

    /**
     * 输出debug level的log信息.
     *
     * @param logger  日志记录器
     * @param message log信息,如:<code>xxx{0},xxx{1}...</code>
     * @param params  log格式化参数,数组length与message参数化个数相同, 如: <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     */
    public static void debug(Logger logger, String message, Object... params) {
        if (logger.isDebugEnabled()) {
            logger.debug(format(message, params));
        }
    }

    /**
     * 输出debug level的log信息.
     *
     * @param throwable 异常对象
     * @param logger    日志记录器
     * @param message   log信息,如:<code>xxx{0},xxx{1}...</code>
     * @param params    log格式化参数,数组length与message参数化个数相同, 如: <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     */
    public static void debug(Throwable throwable, Logger logger, String message, Object... params) {
        if (logger.isDebugEnabled()) {
            logger.debug(format(message, params), throwable);
        }
    }

    /**
     * 输出error level的log信息.
     *
     * @param logger  日志记录器
     * @param message log信息,如:<code>xxx{0},xxx{1}...</code>
     * @param params  log格式化参数,数组length与message参数化个数相同, 如: <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     */
    public static String error(Logger logger, String message, Object... params) {
        String msg = format(message, params);
        if (logger.isErrorEnabled()) {
            logger.error(msg);
        }
        return msg;
    }

    /**
     * 输出error level的log信息.
     *
     * @param throwable 异常对象
     * @param logger    日志记录器
     * @param message   log信息,如:<code>xxx{0},xxx{1}...</code>
     * @param params    log格式化参数,数组length与message参数化个数相同, 如: <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     */
    public static String error(Throwable throwable, Logger logger, String message, Object... params) {
        String msg = format(message, params);
        if (logger.isErrorEnabled()) {
            logger.error(msg, throwable);
        }
        return msg;
    }

    /**
     * 日志信息参数化格式化
     *
     * @param message log信息,如:<code>xxx{0},xxx{1}...</code>
     * @param params  log格式化参数,数组length与message参数化个数相同, 如: <code>Object[]  object=new Object[]{"xxx","xxx"}</code>
     * @return 格式化后的日志信息
     */
    public static String format(String message, Object... params) {
        if (params != null && params.length != 0) {
            ArrayList<String> list = new ArrayList<String>();
            for (Object p : params) {
                if (p != null) {
                    //list.add(p.toString());
                    // 把对象转成json字符串, 方便查询对象属性
                    list.add(JSON.toJSONStringWithDateFormat(p, DEFAULT_DATE_PATTERN));
                } else {
                    list.add("");
                }
            }
            return formatMessage(message, list.toArray());
        }
        return message;

    }

    public static String formatMessage(String message, Object[] params) {
        if ((message == null) || (params == null) || (params.length == 0)) {
            return message;
        }

        return MessageFormat.format(message, params);
    }

}